package com.itv.email;

import com.itv.email.bean.ADBean;
import com.itv.email.bean.EmailInfo;
import com.itv.email.bean.Html;
import com.itv.email.data.ReadPreprocessData;
import com.itv.email.pool.SendFactory;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * User: bjxiajun
 * Date: 14-5-22
 * Time: 下午4:55
 * 发送
 */
public class SendMail {
    private static Logger logger = Logger.getLogger(SendMail.class);
    private static Random random = new Random();

    public static void main(String[] args) throws Exception {
        SendMail.getADUser(null, null, null);
        Thread.sleep(2000);
    }

    public static void getADUser(String userPath, String c32c3Path, String adPath) throws IOException {
        userPath = userPath == null ? "order.txt" : userPath;
        File file = new File(userPath);
        if (!file.exists()) {
            throw new FileNotFoundException("文件[" + userPath + "]没有找到.");
        }
        SendFactory email = SendFactory.getInstence();
        Map<String, String> c3_c3 = ReadPreprocessData.getCategoryMap(c32c3Path);//分类对应
        Map<String, List<ADBean>> rec_sku = ReadPreprocessData.getADData(adPath);//推荐商品信息
        List<Integer> index = new ArrayList<Integer>(9);//保存一推的下标
        InputStream is = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            String[] info = line.split("\t");
            if (info.length >= 6) {
                EmailInfo ei = new EmailInfo();
                String c2 = info[1];
                String c3 = info[3];
                String c3name = info[4];
                String sub = null;
                ei.setRecAddress(info[5]);
                ei.setType("html");
                int mRecSize = 6;
                int sRecSize = 3;
                String rec_c3 = c3_c3.get(c3);
                if (rec_c3 == null || "".equals(rec_c3)) {
                    mRecSize = 9;
                    sRecSize = 0;
                }
                List<ADBean> recList = rec_sku.get(c3);
                Random mr = new Random();
                String html = Html.html;
                for (int i = 1; i < mRecSize + 1; i++) {
                    int j = mr.nextInt(recList.size());
                    if (index.contains(j)) {
                        i--;
                        continue;
                    }
                    index.add(j);
                    ADBean ad = recList.get(j);
                    sub = ad.getTilte();
                    html = replace(ad, html, i);
                }
                ei.setSubject("#"+sub+"#【" + SendMail.random.nextInt(10000)+"期】时尚精选");
                index.clear();

                recList = rec_sku.get(rec_c3);
                for (int i = 7; i < sRecSize + 7; i++) {
                    int j = mr.nextInt(recList.size());
                    if (index.contains(j)) {
                        i--;
                        continue;
                    }
                    index.add(j);
                    ADBean ad = recList.get(j);
                    html = replace(ad, html, i);
                }
                index.clear();
                recList = rec_sku.get(c2);
                for (int i = 10; i < 15; i++) {
                    int j = mr.nextInt(recList.size());
                    if (index.contains(j)) {
                        i--;
                        continue;
                    }
                    index.add(j);
                    ADBean ad = recList.get(j);
                    html = replace(ad, html, i);
                }
                ei.setInfo(html);
                boolean isSend = email.send(ei);
                //System.out.println(html);
                System.out.println("发送邮件到：" + ei.getRecAddress() + " " + isSend);
                if (isSend) {
                    logger.debug(ei.getRecAddress());
                }
            }
        }
        is.close();
        isr.close();
    }

    private static String replace(ADBean ad, String html, int i) {
        String url = "/url" + i + "/";
        html = html.replaceAll(url, ad.getUrlId());
        html = html.replaceAll("/img" + i + "/", ad.getImg());
        html = html.replaceAll("/title" + i + "/", ad.getTilte());
        html = html.replaceAll("/oldPric" + i + "/", ad.getOldPric());
        html = html.replaceAll("/newPric" + i + "/", ad.getNewPric());
        return html;
    }
}
